home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3834 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.5 KB  |  83 lines

  1. Path: news.lainet.com!usenet
  2. From: jason@lainet.com (Jason L. Pollack)
  3. Newsgroups: comp.lang.c++
  4. Subject: Crashing bug in constructor w/new
  5. Date: 26 Jan 1996 06:17:52 GMT
  6. Organization: LA Internet (310)442-4670
  7. Message-ID: <4e9rmg$af0@lainet2.lainet.com>
  8. NNTP-Posting-Host: a2p10r.lainet.com
  9. Mime-Version: 1.0
  10.  
  11. I'm having a hard time with this problem.  There is a crash when I try
  12. to new within a constructor.  I'm not sure if this is a bug in the
  13. compiler, a restriction of the language, or a bug in the programmer's
  14. wetware.
  15.  
  16. I have a class that contains objects of another class whose constructor
  17. puts it on a linked list, allocating a new node on the list with new.
  18.  
  19. It looks something like this:
  20.  
  21. class ClassX {
  22.   ClassY array[15];
  23.   //...and other stuff, of course.
  24. };
  25.  
  26. class ClassY {
  27.   ClassY() {
  28.   YController::Add(this);
  29.   };
  30.   ~ClassY();
  31.   //...and other stuff...
  32. };
  33.  
  34. class YController {
  35.   static node * list;
  36.   static void Add(ClassY* item)
  37.      {
  38.      //bounds checking and normal linked list stuff here...
  39.      node * n = new node(item,NULL);
  40.      list = n;
  41.      }
  42.   static class node {
  43.      node(ClassY * it, node* nx) { item = it; nx = next; }
  44.      ClassY * item;
  45.      node * next;
  46.      };
  47. };
  48. YController::node * YController::list = NULL;
  49.     
  50.  
  51. This is the bare shell of my program.  There is a lot of code missing
  52. that handles the linked list, but I want to draw your attention to the 
  53. line containing new in YController::Add().
  54.  
  55. In my program, I do this:
  56.  
  57.   ClassX xx = new ClassX;
  58.  
  59. And the program crashes.  I've traced it down to the constructor of
  60. the 15th element in array[].  The linked list of ClassY items is
  61. correct until it constructs array[14].  What's happening is that new
  62. is allocating nodes where it should have already allocated memory
  63. for ClassX!  As I trace the program, I see that it is correctly
  64. maintaining the list as I expect, but when it allocates array[14], it
  65. puts the object somewhere in the list and corrupts memory.
  66.  
  67. Clearly it had no choice where to put array[14], it's part of the space
  68. of ClassX.  I want to know why it put the beginning of my linked list 
  69. (the first node, or any node for that matter) in a part of memory it
  70. already knew xx must reside.
  71.  
  72. I'm using Microsoft's VC++1.5 on a Win95 system.
  73.  
  74. Does anyone know what is causing this problem?  I've used new to
  75. allocate memory in plenty of other constructors before; I'm surprised
  76. at this behavior.  Is it a compiler bug, or is there something more
  77. subtle that I'm missing?
  78.  
  79. Thanks for your help,
  80. Jay
  81.  
  82.  
  83.